﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    using System.Security.Permissions;
    using System.Collections.Generic;

    /// <summary>
    /// 管理后台顶部控件。
    /// </summary>
    /// <remarks>管理后台顶部控件。</remarks>
    [ToolboxItemFilter("GSC.WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundHeadControl runat=server height=48px width=100%></{0}:BackgroundHeadControl>")]
    [SRDescription("Manage_Head", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundHeadControlDesigner))]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    //[PersistChildren(false)]
    //[DefaultProperty("Items")]
    //[ParseChildren(true, "Items")]
    //[ToolboxBitmap(typeof(BackgroundHeadControl), "BackgroundHeadControl.bmp")] 
    public class BackgroundHeadControl : DataBoundControl, INamingContainer, ICompositeControlDesignerAccessor, IAsyncControlIDsExtender
    {
        #region IAsyncControlIDsExtender
        /// <summary>
        /// 
        /// </summary>
        List<string> IAsyncControlIDsExtender.ControlIDs
        {
            get
            {
                List<string> ids = new List<string>();
                if (this.AutomaticLoadControl)
                {
                    foreach (BackgroundHeadControlItem item in this.Items)
                    {
                        string asxc = item.Ascx;
                        if (asxc != string.Empty && asxc.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                        {
                            ids.Add(item.LinkButton.ClientID);
                        }
                    }
                }
                return ids;
            }
        }
        #endregion
        #region ICompositeControlDesignerAccessor
        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        #endregion
        #region IStateManager
        /// <summary>
        /// 
        /// </summary>
        /// <param name="savedState"></param>
        protected override void LoadViewState(object savedState)
        {
            object baseState = null;
            object[] myState = null;
            if (savedState != null)
            {
                myState = (object[])savedState;
                if (myState.Length != 2)
                {
                    throw new ArgumentException("Invalid view state");
                }
                baseState = myState[0];
            }
            base.LoadViewState(baseState);
            if ((myState != null) && (myState[1] != null))
            {
                ((IStateManager)Items).LoadViewState(myState[1]);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override object SaveViewState()
        {
            object[] oo = new object[] { base.SaveViewState(), null };
            if ((_items != null) && (_items.Count > 0))
            {
                oo[1] = ((IStateManager)_items).SaveViewState();
            }
            if (oo[0] == null && oo[1] == null) return null;
            return oo;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void TrackViewState()
        {
            base.TrackViewState();
            if (this._items != null)
            {
                ((IStateManager)_items).TrackViewState();
            }
        }
        #endregion
        private bool _hasItem;
        private Table _table;
        TableRow _row;
        TemplateTableCellOwner _logoCell;
        TemplateTableCellOwner _logoutCell;
        #region 重写基类。
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        protected override void AddParsedSubObject(object obj)
        {
            if (obj is BackgroundHeadControlItem)
            {
                this.Items.Add((BackgroundHeadControlItem)obj);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            _table = new Table();
            _table.ID = "Table";
            _hasItem = Items.Count > 0;
            //创建行。
            this._row = new TableRow();
            this._row.ID = "Row";
            _table.Rows.Add(this._row);
            //创建logo例。
            this._logoCell = new TemplateTableCellOwner();
            _logoCell.ID = "LogoCell";
            this._row.Cells.Add(this._logoCell);
            //创建logo例模板。
            ITemplate tempLogo = _logoTemplate;
            if (tempLogo == null)
            {
                tempLogo = new DefaultEmptyTemplate();
            }
            tempLogo.InstantiateIn(this._logoCell);
            //创建item例。
            _itemCells = new List<TableCell>();
            int i = 0;
            foreach (BackgroundHeadControlItem item in this.Items)
            {
                TableCell cell = new TableCell();
                cell.ID = "ItemCell" + i.ToString();
                _itemCells.Add(cell);
                cell.Controls.Add(item);
                this._row.Cells.Add(cell);
                i++;
            }
            //
            //创建logout例。
            this._logoutCell = new TemplateTableCellOwner(this);
            this._logoutCell.ID = "LogoutCell";
            this._row.Cells.Add(this._logoutCell);
            //创建logo例模板。
            ITemplate tempLogout = _logoutTemplate;
            if (tempLogout == null)
            {
                tempLogout = new BackgroundHeadControlLogoutDefaultTemplate();
            }
            tempLogout.InstantiateIn(this._logoutCell);
            //
            this.Controls.Add(this._table);
            this._logoCell.DataBind();
            this._logoutCell.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            if (args is CommandEventArgs)
            {
                CommandEventArgs ce = (CommandEventArgs)args;
                switch (ce.CommandName)
                {
                    case "HeadLogout":
                        this.OnLogout(ce);
                        if (this.LogoutSessionName != string.Empty)
                        {
                            this.Page.Session.Remove(this.LogoutSessionName);
                            if (this.LogoutUrl != string.Empty)
                            {
                                this.Page.Response.Redirect(this.LogoutUrl, true);
                            }
                        }
                        return true;
                    default:
                        //加载控件。
                        /*
                        if (((Control)source).Parent is IAutomaticLoadControl)
                        {
                            IAutomaticLoadControl item = (IAutomaticLoadControl)((Control)source).Parent;
                            if (this.AutomaticLoadControlID != string.Empty && this.AutomaticLoadControl && item.Ascx != string.Empty && item.Ascx.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                            {
                                Control control = this.Parent.FindControl(this.AutomaticLoadControlID);
                                if (control is IUpdateAscx)
                                {
                                    ((IUpdateAscx)control).UpdateAscx(item.Ascx);
                                }
                            }
                        }
                         */
                        BackgroundHeadControlItem item = (BackgroundHeadControlItem)((Control)source).Parent;
                        BackgroundControl master = null;
                        if (this.AutomaticLoadControlID != string.Empty)
                        {
                            master = this.Parent.FindControl(this.AutomaticLoadControlID) as BackgroundControl;
                        }
                        //
                        if (master == null)
                        {
                            master = BackgroundControl.GetCurrent(this.Page);
                        }
                        //
                        if (master != null)
                        {
                            this.AutomaticLoadControl = master.AutomaticLoadControl;
                            //
                            if (this.AutomaticLoadControl && item.Ascx != string.Empty && item.Ascx.EndsWith(".ascx", StringComparison.InvariantCultureIgnoreCase))
                            {
                                //验证加载的是否为菜单子控件。
                                if (item.IsMenu)
                                {
                                    //加载菜单控件。
                                    ((IUpdateMenu)master).ClearLoadChildMenu();
                                    ((IUpdateMenu)master).UpdateMenuAscx(item.Ascx);
                                }
                                else
                                    //加载内容控件。
                                    master.UpdateAscx(item.Ascx);
                            }
                        }
                        this.OnItemClick(new BackgroundHeadControlItemClickEventArgs(item, ce.CommandName, ce.CommandArgument));
                        return true;
                }
            }
            return base.OnBubbleEvent(source, args);
        }
        /// <summary>
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //
            string l = System.Threading.Thread.CurrentThread.CurrentCulture.Name.Replace("-", "_");
            if (this.MinWidth.IsEmpty)
            { _table.Width = Width; }
            else
            {
                _table.Style["min-width"] = this.MinWidth.ToString();
                if (this.Page.Request.Browser.IsBrowser("IE"))
                {
                    if (this.Page.Request.Browser.MajorVersion == 6)
                    {
                        _table.Style["width"] = "expression(document.documentElement.clientWidth < " + this.MinWidth.Value.ToString() + " ? '" + this.MinWidth.ToString() + "' : '100%')";
                    }
                    else if (this.Page.Request.Browser.MajorVersion == 7)
                    {
                        _table.Style["width"] = "expression(document.body.clientWidth < " + this.MinWidth.Value.ToString() + " ? '" + this.MinWidth.ToString() + "' : '100%')";
                    }
                    else if (this.Page.Request.Browser.MajorVersion >= 8)
                    {
                        _table.Width = Width;
                    }
                }
                else if (this.Page.Request.Browser.IsBrowser("Safari"))
                {
                    //HeadTable.Style["width"] = "expression(Math.min(window.innerWidth, document.body.clientWidth) < " + this.CenterWidth.Value.ToString() + " ? '" + this.CenterWidth.ToString() + "' : '100%')";
                    _table.Width = Width;
                }
                else
                {
                    _table.Width = Width;
                }
            }
            //
            _table.CellPadding = 0;
            _table.CellSpacing = 0;
            _table.BorderWidth = 0;
            _table.BackColor = this.BackColor;
            _table.BorderColor = this.BorderColor;
            _table.BorderStyle = this.BorderStyle;
            _table.BorderWidth = this.BorderWidth;
            _table.Height = Height;
            this._table.Style["table-layout"] = "fixed";
            this._table.Style["background-repeat"] = "repeat-x";
            if (string.IsNullOrEmpty(this.BackImage))
                this._table.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("bg.gif")) + ")";
            else
                this._table.Style["background-image"] = "url(" + this.ResolveClientUrl(this.BackImage) + ")";
            //
            this._row.Height = this.Height;
            //
            this._logoCell.Height = this.Height;
            this._logoCell.Width = LogoCellWidth;
            this._logoCell.Style["overflow"] = "hidden";
            this._logoCell.Style["background-repeat"] = "no-repeat";
            this._logoCell.Style["text-align"] = "left";
            if (string.IsNullOrEmpty(this.LogoBackImage))
                this._logoCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("logobg.gif")) + ")";
            else
                this._logoCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.LogoBackImage) + ")";
            //
            foreach (TableCell item in this._itemCells)
            {
                item.Height = this.Height;
                item.Width = this.ItemWidth;
                item.Style["text-align"] = "left";
                item.Style["overflow"] = "hidden";
                item.Style["background-repeat"] = "repeat-x";
                item.Style["background-position"] = "left top";
                if (string.IsNullOrEmpty(this.ItemBackImage))
                    item.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("itembg.gif")) + ")";
                else
                    item.Style["background-image"] = "url(" + this.ResolveClientUrl(this.ItemBackImage) + ")";
            }
            //
            foreach (BackgroundHeadControlItem item in this.Items)
            {
                //
                item.LinkButton.CausesValidation = false;
                if (this.Page.Request.Browser.Browser.Equals("Firefox", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (this.ItemPaddingTop.IsEmpty)
                        item.TopPanel.Height = this.ItemPaddingTop;
                    else
                        item.TopPanel.Height = Unit.Pixel((int)(this.ItemPaddingTop.Value - 3));
                }
                else
                {
                    item.TopPanel.Height = this.ItemPaddingTop;
                }
                item.TopPanel.Style["overflow"] = "hidden";
                item.TopPanel.Style["font-size"] = "0px";
                //
                item.Height = this.Height;
                item.Width = this.ItemWidth;
                item.Style["text-align"] = "center";
                item.Style["overflow"] = "hidden";
                item.Style["background-repeat"] = "no-repeat";
                item.Style["background-position"] = "left top";
                if (string.IsNullOrEmpty(this.SecludeBackImage))
                    item.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("seclude.gif")) + ")";
                else
                    item.Style["background-image"] = "url(" + this.ResolveClientUrl(this.SecludeBackImage) + ")";
                //
            }
            //
            this._logoutCell.Height = this.Height;
            this._logoutCell.Style["width"] = "auto";
            //this._logoutCell.Style["word-wrap"] = "keep-all";
            this._logoutCell.Style["background-repeat"] = "no-repeat";
            this._logoutCell.Style["background-position"] = "left top";
            this._logoutCell.Style["vertical-align"] = "middle";
            this._logoutCell.Style["text-align"] = "right";
            this._logoutCell.Style["padding-right"] = "8px";
            this._logoutCell.Style["padding-left"] = "8px";
            this._logoutCell.Style["overflow"] = "hidden";
            if (string.IsNullOrEmpty(this.SecludeBackImage))
                this._logoutCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("seclude.gif")) + ")";
            else
                this._logoutCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.SecludeBackImage) + ")";
            foreach (Control item in this.LogoutCell.Controls)
            {
                if (item is ImageButton && ((ImageButton)item).CommandName == "HeadLogout")
                {
                    if (string.IsNullOrEmpty(this.LogoutImageUrl))
                    {
                        ((ImageButton)item).ImageUrl = this.GetWebResourceUrl(l + ".logout.gif");
                    }
                    else
                    {
                        ((ImageButton)item).ImageUrl = this.LogoutImageUrl;
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
        /// <summary>
        /// 获取控件宽度。
        /// </summary>
        /// <remarks>获取控件宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(false)]
        public new Unit Width
        {
            get
            {
                return Unit.Percentage(100);
            }
        }
        /// <summary>
        /// 获取控件高度。
        /// </summary>
        /// <remarks>获取控件高度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(false)]
        public new Unit Height
        {
            get
            {
                return Unit.Pixel(48);
            }
        }
        #endregion
        BackgroundHeadControlItemCollection _items = null;
        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        public virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundHeadControl), "GSC.WebControls.BackgroundHeadControl.Images." + resourceUrl);
        }
        /// <summary>
        /// 获取按键例的集合。
        /// </summary>
        /// <remarks>获取按键例的集合。</remarks>
        /// <value>GuoShuCan.SDK.Web.Controls.HeadItemCollection</value>
        [
        PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorAttribute(typeof(BackgroundHeadControlItemCollectionEditor), typeof(UITypeEditor)),
        NotifyParentProperty(true)
        ]
        [Category("Behavior")]
        [SRDescription("Manage_Head_Items", typeof(Properties.Resources))]
        public BackgroundHeadControlItemCollection Items
        {
            get
            {
                if (_items == null)
                {
                    _items = new BackgroundHeadControlItemCollection();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_items).TrackViewState();
                    }
                }
                return _items;
            }
        }
        ITemplate _logoTemplate;
        /// <summary>
        /// 获取或设置 logo 模板对象。
        /// </summary>
        /// <remarks>获取或设置 logo 模板对象。</remarks>
        /// <value>System.Web.UI.ITemplate</value>
        [
        Browsable(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        DefaultValue(typeof(ITemplate), null),
        Description(""),
        TemplateContainer(typeof(BackgroundHeadControl))
        ]
        public ITemplate LogoTemplate
        {
            get
            {
                return _logoTemplate;
            }
            set
            {
                _logoTemplate = value;
            }
        }
        /// <summary>
        /// 获取或设置 Logo 背景图片。
        /// </summary>
        /// <remarks>获取或设置 Logo 背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_Head_LogoBackImage", typeof(Properties.Resources))]
        public string LogoBackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }

        /// <summary>
        /// 获取或设置背景图片。
        /// </summary>
        /// <remarks>获取或设置背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_Head_BackImage", typeof(Properties.Resources))]
        public string BackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }
        ITemplate _logoutTemplate;
        /// <summary>
        /// 获取或设置 HeadLogout 模板对象。
        /// </summary>
        /// <remarks>获取或设置 HeadLogout 模板对象。</remarks>
        /// <value>System.Web.UI.ITemplate</value>
        [
               Browsable(false),
               PersistenceMode(PersistenceMode.InnerProperty),
               DefaultValue(typeof(ITemplate), null),
               Description(""),
               TemplateContainer(typeof(BackgroundHeadControl))
               ]
        public ITemplate LogoutTemplate
        {
            get
            {
                return _logoutTemplate;
            }
            set
            {
                _logoutTemplate = value;
            }
        }
        /// <summary>
        /// 获取或设置菜单分隔背景图片。
        /// </summary>
        /// <remarks>获取或设置菜单分隔背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_Head_SecludeBackImage", typeof(Properties.Resources))]
        public string SecludeBackImage
        {
            get
            {
                object o = this.ViewState["SecludeBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["SecludeBackImage"] = value;
            }
        }

        /// <summary>
        /// 获取或设置注销按钮图标。
        /// </summary>
        /// <remarks>获取或设置注销按钮图标。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_Head_LogoutImageUrl", typeof(Properties.Resources))]
        public string LogoutImageUrl
        {
            get
            {
                object o = this.ViewState["LogoutImageUrl"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["LogoutImageUrl"] = value;
            }
        }

        /// <summary>
        /// 获取行对象。
        /// </summary>
        /// <remarks>获取行对象。</remarks>
        /// <value>System.Web.UI.WebControls.TableRow</value>
        [Browsable(false)]
        public TableRow Row
        {
            get
            {
                return _row;
            }
        }

        /// <summary>
        /// 获取 HeadLogout 例对象。
        /// </summary>
        /// <remarks>获取 HeadLogout 例对象。</remarks>
        /// <value>TemplateTableCellOwner</value>
        [Browsable(false)]
        public TemplateTableCellOwner LogoutCell
        {
            get
            {
                return _logoutCell;
            }
        }

        /// <summary>
        /// 获取 Logo 例对象。
        /// </summary>
        /// <remarks>获取 Logo 例对象。</remarks>
        /// <value>TemplateTableCellOwner</value>
        [Browsable(false)]
        public TemplateTableCellOwner LogoCell
        {
            get
            {
                return _logoCell;
            }
        }

        /// <summary>
        /// 按钮对象宽度。
        /// </summary>
        /// <remarks>按钮对象宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "68px")]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Layout")]
        [SRDescription("Manage_Head_ItemWidth", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public virtual Unit ItemWidth
        {
            get
            {
                object o = this.ViewState["ItemWidth"];
                if (o == null) return Unit.Pixel(68);
                return (Unit)o;
            }
            set
            {
                this.ViewState["ItemWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置对象背景图片。
        /// </summary>
        /// <remarks>获取或设置对象背景图片。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [SRDescription("Manage_Head_ItemBackImage", typeof(Properties.Resources))]
        public string ItemBackImage
        {
            get
            {
                object o = this.ViewState["ItemBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["ItemBackImage"] = value;
            }
        }
        private static readonly object _logoutObject = new object();
        /// <summary>
        /// 注销登录事件。
        /// </summary>
        /// <remarks>注销登录事件。</remarks>
        [Browsable(true)]
        [Category("Behavior")]
        [SRDescription("Manage_Head_Logout", typeof(Properties.Resources))]
        public event CommandEventHandler Logout
        {
            add
            {
                Events.AddHandler(_logoutObject, value);
            }
            remove
            {
                Events.RemoveHandler(_logoutObject, value);
            }
        }
        /// <summary>
        /// 注销登录事件。
        /// </summary>
        /// <remarks>注销登录事件。</remarks>
        /// <param name="e"></param>
        protected virtual void OnLogout(CommandEventArgs e)
        {
            CommandEventHandler handler = (CommandEventHandler)Events[_logoutObject];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        private static readonly object _itemClickObject = new object();
        /// <summary>
        /// 头对象按钮点击事件。
        /// </summary>
        /// <remarks>头对象按钮点击事件。</remarks>
        [Browsable(true)]
        [Category("Behavior")]
        [SRDescription("Manage_Head_ItemClick", typeof(Properties.Resources))]
        public event BackgroundHeadControlItemClickEventHandler ItemClick
        {
            add
            {
                Events.AddHandler(_itemClickObject, value);
            }
            remove
            {
                Events.RemoveHandler(_itemClickObject, value);
            }
        }
        /// <summary>
        /// 头对象按钮点击事件。
        /// </summary>
        /// <remarks>头对象按钮点击事件。</remarks>
        /// <param name="e"></param>
        protected virtual void OnItemClick(BackgroundHeadControlItemClickEventArgs e)
        {
            BackgroundHeadControlItemClickEventHandler handler = (BackgroundHeadControlItemClickEventHandler)Events[_itemClickObject];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        /// <summary>
        /// 获取或设置内部上边位置。
        /// </summary>
        /// <remarks>获取或设置内部上边位置。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "22px")]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Layout")]
        [SRDescription("Manage_Head_ItemPaddingTop", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public virtual Unit ItemPaddingTop
        {
            get
            {
                object o = this.ViewState["ItemPaddingTop"];
                if (o == null) return Unit.Pixel(22);
                return (Unit)o;
            }
            set
            {
                this.ViewState["ItemPaddingTop"] = value;
            }
        }
        /// <summary>
        /// 获取或设置按钮指令例。
        /// </summary>
        /// <remarks>获取或设置按钮指令例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("DataCommandNameField", typeof(Properties.Resources))]
        public virtual string DataCommandNameField
        {
            get
            {
                object o = this.ViewState["DataCommandNameField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataCommandNameField"] = value;
            }
        }

        /// <summary>
        /// 获取或设置按钮名称例。
        /// </summary>
        /// <remarks>获取或设置按钮名称例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [Bindable(false)]
        [TypeConverter(typeof(DataFieldConverter))]
        [SRDescription("DataTextField", typeof(Properties.Resources))]
        public virtual string DataTextField
        {
            get
            {
                object o = this.ViewState["DataTextField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataTextField"] = value;
            }
        }

        /// <summary>
        /// 获取或设置控件的 ID，数据绑定控件从该控件中检索其数据项列表。
        /// </summary>
        /// <remarks>获取或设置控件的 ID，数据绑定控件从该控件中检索其数据项列表。</remarks>
        /// <value>string</value>
        [RefreshProperties(RefreshProperties.Repaint)]
        [IDReferenceProperty(typeof(DataSourceControl))]
        [TypeConverter(typeof(DataSourceIDConverter))]
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [Bindable(false)]
        [SRDescription("DataSourceID", typeof(Properties.Resources))]
        public override string DataSourceID
        {
            get
            {
                return base.DataSourceID;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.DataTextField = string.Empty;
                    this.DataCommandArgumentField = string.Empty;
                    this.DataCommandNameField = string.Empty;
                    this.DataMember = string.Empty;
                }
                if (base.DataSourceID != value)
                {
                    base.DataSourceID = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置对象，数据绑定控件从该对象中检索其数据项列表。
        /// </summary>
        /// <remarks>获取或设置对象，数据绑定控件从该对象中检索其数据项列表。</remarks>
        /// <value>object</value>
        [Browsable(false)]
        [Bindable(true)]
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (base.DataSource != value)
                {
                    base.DataSource = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 当数据源包含多个不同的数据项列表时，获取或设置数据绑定控件绑定到的数据列表的名称。
        /// </summary>
        /// <remarks>当数据源包含多个不同的数据项列表时，获取或设置数据绑定控件绑定到的数据列表的名称。</remarks>
        /// <value>string</value>
        [RefreshProperties(RefreshProperties.All)]
        [TypeConverter(typeof(DataMemberConverter))]
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [Bindable(false)]
        [SRDescription("DataMember", typeof(Properties.Resources))]
        public override string DataMember
        {
            get
            {
                return base.DataMember;
            }
            set
            {
                if (base.DataMember != value)
                {
                    base.DataMember = value;
                    if (this.Initialized)
                    {
                        this.OnDataPropertyChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 获取或设置按钮附加指令例。
        /// </summary>
        /// <remarks>获取或设置按钮附加指令例。</remarks>
        /// <value>string</value>
        [TypeConverter(typeof(DataFieldConverter))]
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [Bindable(false)]
        [SRDescription("DataCommandArgumentField", typeof(Properties.Resources))]
        public virtual string DataCommandArgumentField
        {
            get
            {
                object o = this.ViewState["DataCommandArgumentField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataCommandArgumentField"] = value;
            }
        }
        string dataUrls = string.Empty;
        string dataTexts = string.Empty;
        string dataImages = string.Empty;
        /// <summary>
        /// </summary>
        /// <param name="data"></param>
        protected override void PerformDataBinding(System.Collections.IEnumerable data)
        {
            if (data == null)
                return;
            int index = 0;
            this.Items.Clear();
            //键值
            string _DataTextField = this.DataTextField;
            string _DataCommandArgumentField = this.DataCommandArgumentField;
            string _DataCommandNameField = this.DataCommandNameField;
            string _DataToolTipField = this.DataToolTipField;
            //
            foreach (object dataItem in data)
            {
                BackgroundHeadControlItem item = new BackgroundHeadControlItem();
                //
                if (!string.IsNullOrEmpty(_DataTextField))
                {
                    item.Text = DataBinder.GetPropertyValue(dataItem, _DataTextField, null);
                }
                //
                if (!string.IsNullOrEmpty(_DataCommandArgumentField))
                {
                    item.CommandArgument = DataBinder.GetPropertyValue(dataItem, _DataCommandArgumentField, null);
                }
                //
                if (!string.IsNullOrEmpty(_DataCommandNameField))
                {
                    item.CommandName = DataBinder.GetPropertyValue(dataItem, _DataCommandNameField, null);
                }
                //
                if (!string.IsNullOrEmpty(_DataToolTipField))
                {
                    item.ToolTip = DataBinder.GetPropertyValue(dataItem, _DataToolTipField, null);
                }
                this.Items.Add(item);
                index++;
            }
            base.ChildControlsCreated = false;
            this.EnsureChildControls();
        }
        /// <summary>
        /// 获取控件表对象。
        /// </summary>
        /// <remarks>获取控件表对象。</remarks>
        /// <value>System.Web.UI.WebControls.Table</value>
        [Browsable(false)] 
        public System.Web.UI.WebControls.Table Table
        {
            get
            {
                return _table;
            }
        }
        /// <summary>
        /// 获取是否存在对象。
        /// </summary>
        /// <remarks>获取是否存在对象。</remarks>
        /// <value>bool</value>
        [Browsable(false)] 
        public bool HasItem
        {
            get
            {
                return _hasItem;
            }
        }
        private List<TableCell> _itemCells;

        /// <summary>
        /// 获取对象例集合。
        /// </summary>
        /// <remarks>获取对象例集合。</remarks>
        /// <value>System.Collections.Generic.List&lt;System.Web.UI.WebControls.TableCell&gt;</value>
        [Browsable(false)]  
        public System.Collections.Generic.List<System.Web.UI.WebControls.TableCell> ItemCells
        {
            get
            {
                return _itemCells;
            }
        }
        /// <summary>
        /// 获取或设置当鼠标指针悬停在按钮上时显示的文本例。
        /// </summary>
        /// <remarks>获取或设置当鼠标指针悬停在按钮上时显示的文本例。</remarks>
        /// <value>string</value>
        [Themeable(false)]
        [Browsable(true)]
        [DefaultValue("")]
        [Category("Data")]
        [TypeConverter(typeof(DataFieldConverter))]
        [Bindable(false)]
        [SRDescription("Manage_Head_DataToolTipField", typeof(Properties.Resources))]
        public virtual string DataToolTipField
        {
            get
            {
                object o = this.ViewState["DataFlagField"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["DataFlagField"] = value;
            }
        }

        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }

        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// 获取或设置最小宽度。
        /// </summary>
        /// <remarks>获取或设置最小宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "")]
        [Browsable(true)]
        [Category("Layout")]
        [Bindable(true)]
        [SRDescription("Manage_Head_MinWidth", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public Unit MinWidth
        {
            get
            {
                object o = this.ViewState["MinWidth"];
                if (o == null) return Unit.Empty;
                return (Unit)o;
            }
            set
            {
                this.ViewState["MinWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置客户端登陆会话名称，在使用注销登录事件中使用。
        /// </summary>
        /// <remarks>获取或设置客户端登陆会话名称，在使用注销登录事件中使用。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Browsable(true)]
        [Category("Behavior")]
        [Bindable(true)]
        [SRDescription("Manage_Head_LogoutSessionName", typeof(Properties.Resources))]
        public string LogoutSessionName
        {
            get
            {
                object o = this.ViewState["LogoutSessionName"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["LogoutSessionName"] = value;
            }
        }

        /// <summary>
        /// 获取或设置注销后跳转页面。
        /// </summary>
        /// <remarks>获取或设置注销后跳转页面。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [Editor(typeof(UrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [Category("Behavior")]
        [SRDescription("Manage_Head_LogoutUrl", typeof(Properties.Resources))]
        public string LogoutUrl
        {
            get
            {
                object o = this.ViewState["LogoutUrl"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["LogoutUrl"] = value;
            }
        }

        /// <summary>
        /// 获取或设置自动加载控件对象。
        /// </summary>
        /// <remarks>获取或设置自动加载控件对象。</remarks>
        /// <value>string</value>
        [DefaultValue("")]
        [TypeConverter(typeof(ControlIDConverter))]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("AutomaticLoadControlID", typeof(Properties.Resources))]
        public string AutomaticLoadControlID
        {
            get
            {
                object o = this.ViewState["AutomaticLoadControlID"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["AutomaticLoadControlID"] = value;
            }
        }

        /// <summary>
        /// 获取或设置是否自动加载菜单对应的控件。
        /// </summary>
        /// <remarks>获取或设置是否自动加载菜单对应的控件。</remarks>
        /// <value>bool</value>
        [DefaultValue(false)]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        [SRDescription("AutomaticLoadControl", typeof(Properties.Resources))]
        public bool AutomaticLoadControl
        {
            get
            {
                object o = this.ViewState["AutomaticLoadControl"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["AutomaticLoadControl"] = value;
            }
        }

        /// <summary>
        /// 获取或设置 Logo 例宽度。
        /// </summary>
        /// <remarks>获取或设置 Logo 例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [DefaultValue(typeof(Unit), "188px")]
        [Browsable(true)]
        [Category("Layout")]
        [Bindable(true)]
        [SRDescription("Manage_Head_LogoCellWidth", typeof(Properties.Resources))]
        [TypeConverter(typeof(UnitConverter))]
        public Unit LogoCellWidth
        {
            get
            {
                object o = this.ViewState["LogoCellWidth"];
                if (o == null) return Unit.Pixel(188);
                return (Unit)o;
            }
            set
            {
                this.ViewState["LogoCellWidth"] = value;
            }
        }
    }
}